package DT8;

import java.sql.ResultSet;
import java.text.DecimalFormat;
import javagprssrv.Common.DateTime;
import javagprssrv.DataBase.DBSQLExecute;
import javagprssrv.JGS_Param;
import javagprssrv.StructClass.structTask;
import javagprssrv.StructClass.structRecvData;

public class TrdTCPDataSaver_DT8 extends java.lang.Thread
{
    javagprssrv.StructClass.structRecvData recvData;
    javagprssrv.StructClass.structTask task;
    int JobIndex;
    java.lang.String _Protocol;
    int _UDPServerSocketIndex;
    java.lang.String _RemoteIP;
    int _RemotePort;
    java.lang.String _Data;
    java.lang.String _OriginRecvData;
    int RecvDataType;
    java.lang.String TaskID;
    private java.lang.String TCP_TermType;
    int DataType;
    java.lang.String TermNo;
    java.lang.String YBH;
    java.lang.String AsynTelNo;
    java.lang.String AsynTelNo_17;
    java.lang.String DYDJ;
    java.lang.String DYDW;
    java.lang.String zyxs;
    java.lang.String csxs;
    java.lang.String cxxs;
    java.lang.String zdzs;
    java.lang.String zxzs;
    java.lang.String hgs;
    double zdz;
    double zxz;
    double pjz;
    double z00;
    double z01;
    double z02;
    double z03;
    double z04;
    double z05;
    double z06;
    double z07;
    double z08;
    double z09;
    double z10;
    double z11;
    double z12;
    double z13;
    double z14;
    double z15;
    double z16;
    double z17;
    double z18;
    double z19;
    double z20;
    double z21;
    double z22;
    double z23;
    java.lang.String sYB_BL;
    java.lang.String szdz;
    java.lang.String szxz;
    java.lang.String spjz;
    java.lang.String sz00;
    java.lang.String sz01;
    java.lang.String sz02;
    java.lang.String sz03;
    java.lang.String sz04;
    java.lang.String sz05;
    java.lang.String sz06;
    java.lang.String sz07;
    java.lang.String sz08;
    java.lang.String sz09;
    java.lang.String sz10;
    java.lang.String sz11;
    java.lang.String sz12;
    java.lang.String sz13;
    java.lang.String sz14;
    java.lang.String sz15;
    java.lang.String sz16;
    java.lang.String sz17;
    java.lang.String sz18;
    java.lang.String sz19;
    java.lang.String sz20;
    java.lang.String sz21;
    java.lang.String sz22;
    java.lang.String sz23;
    java.lang.String YB_YBH;
    double YB_BL;
    int YB_BBXSW;
    java.lang.String YB_GSMC;
    java.lang.String YB_floatGS;
    java.lang.String s2_Parameter;
    java.lang.String s2_Year;
    java.lang.String s2_Month;
    java.lang.String s2_Date;
    java.lang.String _SJM_DataTime;
    java.lang.String _TJS_ZYXS;
    java.lang.String _TJS_HGS;
    java.lang.String _TJS_CSXS;
    java.lang.String _TJS_CXXS;
    java.lang.String _DYZ_PJZ;
    java.lang.String _DYZ_Min;
    java.lang.String _SJM_MinTime;
    java.lang.String _DYZ_Max;
    java.lang.String _SJM_MaxTime;
    
    public TrdTCPDataSaver_DT8() {
        super(TrdTCPDataSaver_DT8.class.getSimpleName());
        this.JobIndex = -1;
        this.RecvDataType = -1;
        this.TCP_TermType = "8";
        this.TermNo = "";
        this.YBH = "";
        this.AsynTelNo = "";
        this.AsynTelNo_17 = "";
        this.DYDJ = "";
        this.DYDW = "";
    }
    
    private boolean findRecvData() {
        boolean res = false;
        synchronized (javagprssrv.JGS_Param.UnSavedRecvData_TCP) {
            for (int i = 0; i < javagprssrv.JGS_Param.UnSavedRecvData_TCP.size(); ++i) {
                if (javagprssrv.JGS_Param.UnSavedRecvData_TCP.elementAt(i).RecvDataType == java.lang.Integer.valueOf(this.TCP_TermType)) {
                    this.recvData = javagprssrv.JGS_Param.UnSavedRecvData_TCP.remove(i);
                    res = true;
                    break;
                }
            }
            if (javagprssrv.JGS_Param.UnSavedRecvData_TCP.size() > 10000) {
                javagprssrv.JGS_Param.UnSavedRecvData_TCP.clear();
            }
        }
        this._Protocol = this.recvData.Protocol.trim();
        this._RemoteIP = this.recvData.RemoteIP;
        this._RemotePort = this.recvData.RemotePort;
        this._Data = this.recvData.Data;
        this._OriginRecvData = this.recvData.Data;
        return res;
    }
    
    @java.lang.Override
    public void run() {
        try
        {
          while (true)
          {
            Thread.sleep(20L);
            if (findRecvData())
            {
              int cmdType = this._Data.charAt(25);
              if (cmdType == 3)
              {
                this.DataType = this._Data.charAt(26);
                if (checkData())
                {
                  if (this.DataType == 6) {
                    save_03();
                  }
                  if (this.DataType == 5) {
                    save_04();
                  }
                }
              }
            }
          }
        }
        catch (Exception e) {}
    }
    //数据库不符
    public void save_03() throws java.lang.Exception {
        this.ReadRecvDataGetValue();
        final int MonthDataType = 1;
        java.lang.String sql = "select count(*) from T_YSJ where 仪表号='" + this.YB_YBH + "' and 年月='20" + this.s2_Year + this.s2_Month + "'";
        final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        final java.sql.ResultSet rs = dbse.SQL_Query(0, sql);
        rs.next();
        final int recordCounts = rs.getInt(1);
        java.lang.String _zyxs = "";
        final java.lang.String time = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
        if (recordCounts == 0) {
            if (java.lang.Integer.valueOf(this.zyxs) == 0) {
                _zyxs = "1";
            }
            else {
                _zyxs = this.zyxs;
            }
            sql = "insert into T_YSJ(年月,仪表号,保存日期,总运行时,超上限时,超下限时,最大值,最大值时,最小值,最小值时,平均值,种类,合格时,合格率,超上限率,超下限率) values (";
            sql = sql + "'20" + this.s2_Year + this.s2_Month + "','" + this.YB_YBH + "','" + time + "',";
            sql = sql + this.zyxs + "," + this.csxs + "," + this.cxxs + "," + this.szdz + ",'" + this.zdzs + "'," + this.szxz + ",'" + this.zxzs + "',";
            sql = sql + this.spjz + "," + java.lang.String.valueOf(MonthDataType) + "," + this.hgs + ",'" + new java.text.DecimalFormat(this.YB_floatGS).format(100.0 * java.lang.Integer.valueOf(this.hgs) / java.lang.Integer.valueOf(_zyxs)) + "',";
            sql = sql + "'" + new java.text.DecimalFormat(this.YB_floatGS).format(100.0 * java.lang.Integer.valueOf(this.csxs) / java.lang.Integer.valueOf(_zyxs)) + "',";
            sql = sql + "'" + new java.text.DecimalFormat(this.YB_floatGS).format(100.0 * java.lang.Integer.valueOf(this.cxxs) / java.lang.Integer.valueOf(_zyxs)) + "')";
        }
        else {
            if (java.lang.Integer.valueOf(this.zyxs) == 0) {
                _zyxs = "1";
            }
            else {
                _zyxs = this.zyxs;
            }
            sql = "update T_YSJ set 年月='20" + this.s2_Year + this.s2_Month + "',仪表号='" + this.YB_YBH + "',保存日期='" + time + "',";
            sql = sql + "总运行时=" + this.zyxs + ",超上限时=" + this.csxs + ",超下限时=" + this.cxxs + ",最大值=" + this.szdz + ",最大值时='" + this.zdzs + "',最小值=" + this.szxz + ",最小值时='" + this.zxzs + "',平均值=" + this.spjz + ",";
            sql = sql + "超上限率='" + new java.text.DecimalFormat(this.YB_floatGS).format(100.0 * java.lang.Integer.valueOf(this.csxs) / java.lang.Integer.valueOf(_zyxs)) + "',";
            sql = sql + "超下限率='" + new java.text.DecimalFormat(this.YB_floatGS).format(100.0 * java.lang.Integer.valueOf(this.cxxs) / java.lang.Integer.valueOf(_zyxs)) + "',";
            sql = sql + "合格率='" + new java.text.DecimalFormat(this.YB_floatGS).format(100.0 * java.lang.Integer.valueOf(this.hgs) / java.lang.Integer.valueOf(_zyxs)) + "',";
            sql = sql + "合格时=" + this.hgs + ",";
            sql = sql + "种类=" + java.lang.String.valueOf(MonthDataType) + " where 仪表号='" + this.YB_YBH + "' and 年月='20" + this.s2_Year + this.s2_Month + "'";
        }
        this.SQLExecute(sql);
    }
    
    //数据库不符
    public void save_04() throws java.lang.Exception {
        this.ReadRecvDataGetValue();
        java.lang.String sql = "select count(*) from T_RSJ where 仪表号='" + this.YB_YBH + "' and 日期='20" + this.s2_Year + this.s2_Month + this.s2_Date + "'";
        final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        final java.sql.ResultSet rs = dbse.SQL_Query(0, sql);
        rs.next();
        final int recordCounts = rs.getInt(1);
        java.lang.String _zyxs = "";
        final java.lang.String time = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
        if (recordCounts == 0) {
            if (java.lang.Integer.valueOf(this.zyxs) == 0) {
                _zyxs = "1";
            }
            else {
                _zyxs = this.zyxs;
            }
            sql = "insert into T_RSJ(日期,仪表号,保存日期,总运行时,超上限时,超下限时,最大值,最大值时,最小值,最小值时,平均值,";
            sql += "种类) values (";
            sql = sql + "'20" + this.s2_Year + this.s2_Month + this.s2_Date + "','" + this.YB_YBH + "','" + time + "',";
            sql = sql + this.zyxs + "," + this.csxs + "," + this.cxxs + "," + this.szdz + ",'" + this.zdzs + "'," + this.szxz + ",'" + this.zxzs + "'," + this.spjz + ",";
            sql = sql + java.lang.String.valueOf(this.DataType) + ")";
        }
        else {
            if (java.lang.Integer.valueOf(this.zyxs) == 0) {
                _zyxs = "1";
            }
            else {
                _zyxs = this.zyxs;
            }
            sql = "update T_RSJ set 日期='20" + this.s2_Year + this.s2_Month + this.s2_Date + "',仪表号='" + this.YB_YBH + "',保存日期='" + time + "',";
            sql = sql + "总运行时=" + this.zyxs + ",超上限时=" + this.csxs + ",超下限时=" + this.cxxs + ",最大值=" + this.szdz + ",最大值时='" + this.zdzs + "',最小值=" + this.szxz + ",最小值时='" + this.zxzs + "',平均值=" + this.spjz + ",";
            sql = sql + " 种类=" + java.lang.String.valueOf(this.DataType) + " where 仪表号='" + this.YB_YBH + "' and 日期='20" + this.s2_Year + this.s2_Month + this.s2_Date + "'";
        }
        this.SQLExecute(sql);
    }
    
    public boolean checkData() {
        boolean res = true;
        this.AsynTelNo_17 = this._Data.substring(8, 25).trim();
        this.AsynTelNo = this.AsynTelNo_17.substring(6);
        if (!this.GetYBInfo()) {
            res = false;
        }
        return res;
    }
    
    //数据库不符
    public boolean GetYBInfo() {
        boolean res = true;
        final java.lang.String sql = "select YB.*,GS.报表小数位,yb2.行政区域码,yb2.通讯方式 from T_YB YB left outer join T_GS GS on GS.公司名称=YB.公司名称 left outer join T_YB2 yb2 on yb2.仪表号=yb.仪表号 where 唯一码='" + this.AsynTelNo + "'";
        final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        final java.sql.ResultSet rs = dbse.SQL_Query(0, sql);
        try {
            rs.next();
            this.YB_YBH = rs.getString("仪表号").trim();
            this.YB_BL = rs.getFloat("倍率");
            this.sYB_BL = rs.getString("倍率");
            this.YB_GSMC = rs.getString("公司名称").trim();
            this.YB_BBXSW = rs.getInt("报表小数位");
            this.DYDJ = rs.getString("电压等级").trim();
            this.DYDW = rs.getString("单位").trim();
            this.YB_BBXSW = 3;
            this.YB_floatGS = "0.";
            for (int i = 0; i < this.YB_BBXSW; ++i) {
                this.YB_floatGS += "0";
            }
            res = true;
        }
        catch (java.lang.Exception e) {
            res = false;
        }
        return res;
    }
    
    public void ReadRecvDataGetValue() throws java.lang.Exception {
        final int idxStart = 0;
        this._SJM_DataTime = this._Data.substring(30, 34);
        this._TJS_ZYXS = this._Data.substring(43, 45);
        this._TJS_HGS = this._Data.substring(45, 47);
        this._TJS_CSXS = this._Data.substring(47, 49);
        this._TJS_CXXS = this._Data.substring(49, 51);
        this._DYZ_PJZ = this._Data.substring(51, 55);
        this._DYZ_Min = this._Data.substring(55, 59);
        this._SJM_MinTime = this._Data.substring(59, 63);
        this._DYZ_Max = this._Data.substring(63, 67);
        this._SJM_MaxTime = this._Data.substring(67, 71);
        this._SJM_DataTime = new DT8.DT8_Util().DT8_GetDatefromSJM(this._SJM_DataTime);
        this._SJM_MinTime = new DT8.DT8_Util().DT8_GetDatefromSJM(this._SJM_MinTime);
        this._SJM_MaxTime = new DT8.DT8_Util().DT8_GetDatefromSJM(this._SJM_MaxTime);
        this.s2_Year = this._SJM_DataTime.substring(2, 4);
        this.s2_Month = this._SJM_DataTime.substring(5, 7);
        this.s2_Date = this._SJM_DataTime.substring(8, 10);
        this.zyxs = java.lang.String.valueOf(this._TJS_ZYXS.charAt(0) + this._TJS_ZYXS.charAt(1) * 'Ā');
        this.hgs = java.lang.String.valueOf(this._TJS_HGS.charAt(0) + this._TJS_HGS.charAt(1) * 'Ā');
        this.csxs = java.lang.String.valueOf(this._TJS_CSXS.charAt(0) + this._TJS_CSXS.charAt(1) * 'Ā');
        this.cxxs = java.lang.String.valueOf(this._TJS_CXXS.charAt(0) + this._TJS_CXXS.charAt(1) * 'Ā');
        this.zxzs = this._SJM_MinTime.substring(5, 7) + this._SJM_MinTime.substring(8, 10) + this._SJM_MinTime.substring(11, 13) + this._SJM_MinTime.substring(14, 16);
        this.zdzs = this._SJM_MaxTime.substring(5, 7) + this._SJM_MaxTime.substring(8, 10) + this._SJM_MaxTime.substring(11, 13) + this._SJM_MaxTime.substring(14, 16);
        this.pjz = this.strDY2doubleDY(this._DYZ_PJZ);
        this.zdz = this.strDY2doubleDY(this._DYZ_Max);
        this.zxz = this.strDY2doubleDY(this._DYZ_Min);
        if (this.DYDW.toUpperCase().equals("KV")) {
            this.YB_BL = 0.001;
        }
        if (this.DYDW.toUpperCase().equals("V")) {
            this.YB_BL = 1.0;
        }
        final java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
        this.szdz = df.format(this.zdz * this.YB_BL);
        this.szxz = df.format(this.zxz * this.YB_BL);
        this.spjz = df.format(this.pjz * this.YB_BL);
    }
    
    private double strDY2doubleDY(final java.lang.String strDY) {
        double res = 0.0;
        final int v0 = strDY.charAt(0);
        final int v0_0 = v0 / 16;
        final int v0_2 = v0 % 16;
        final int v2 = strDY.charAt(1);
        final int v1_0 = v2 / 16;
        final int v1_2 = v2 % 16;
        final int v3 = strDY.charAt(2);
        final int v2_0 = v3 / 16;
        final int v2_2 = v3 % 16;
        final int v4 = strDY.charAt(3);
        final int v3_0 = v4 / 16;
        final int v3_2 = v4 % 16;
        res = v3_0 * 100000 + v3_2 * 10000 + v2_0 * 1000 + v2_2 * 100 + v1_0 * 10 + v1_2 * 1 + v0_0 * 0.1 + v0_2 * 0.01;
        return res;
    }
    
    private int getValue(final java.lang.String _data, final int startIndex, final int endIndex) {
        int res = 0;
        final int lo = _data.charAt(startIndex);
        final int hi = _data.charAt(endIndex);
        res = hi * 255 + lo;
        return res;
    }
    
    public void SQLExecute(final java.lang.String sql) {
        synchronized (javagprssrv.JGS_Param.UnSavedDBSQLString) {
            javagprssrv.JGS_Param.UnSavedDBSQLString.add(sql);
        }
    }
}
